package com.bls.utils;

/**
 * @author Summerday
 */

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Iterator;
import java.util.Set;

/**
 * Jedis工具类
 */
public final class JedisUtil {

    private JedisPool jedisPool;

    public JedisUtil(){

        //获取数据，设置到JedisPoolConfig中
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(20);
        config.setMaxTotal(50);
        config.setMaxWaitMillis(100000);
        //初始化JedisPool
        jedisPool = new JedisPool(config, "192.168.88.63", 6379);

    }


    /**
     * 从jedis连接池中获取获取jedis对象
     */
    public Jedis getJedis() {
        return jedisPool.getResource();
    }


    /**
     * 关闭Jedis
     */
    public static void close(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }

    /**
     * 清空重置Jedis
     */
    public static void reset(Jedis jedis) {
        if (jedis != null) {
            Set<String> keys = jedis.keys("*");
            Iterator<String> iterator = keys.iterator();
            while (iterator.hasNext()){
                String next = iterator.next();
                jedis.del(next);
            }
        }
    }

    /**
     * 查看Jedis
     */
    public static void find(Jedis jedis) {
        if (jedis != null) {
            Set<String> keys = jedis.keys("*");
            Iterator<String> iterator = keys.iterator();
            while (iterator.hasNext()){
                String next = iterator.next();
                System.out.println(next);
            }
        }
    }
}
